package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.config.Upload;
import com.project.entity.po.Img;
import com.project.entity.po.Menu;
import com.project.entity.qo.ImgQo;
import com.project.entity.qo.MenuQo;
import com.project.entity.vo.MenuVo2;
import com.project.exception.ServiceException;
import com.project.mapper.MenuMapper;
import com.project.result.ResponseResult;
import com.project.result.ResultCode;
import com.project.service.MenuService;
import com.project.entity.vo.MenuVo;
import com.project.entity.vo.PageMenuVo;
import com.project.service.UserService;
import com.project.utils.OrderUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qjj
 * @create 2022/3/29 12:13
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuMapper menuMapper;

    @Resource
    private Upload upload;

    @Resource
    private OrderUtils orderUtils;
    /**
     * 获取菜单
     * @return
     */
    @Override
    public List<MenuVo> selectMenuList() {
        List<MenuVo> menuVos = menuMapper.selectMenuList(null,null);
        if (menuVos.size() == 0){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return menuVos;
    }

    /**
     * 分页查询
     * @param current
     * @return
     */
    @Override
    public ResponseResult<Page<MenuVo2>> selectMenuPage(Integer current) {
        Page<MenuVo2> menuVoPage = new Page<>(current,4);
        Page<MenuVo2> menuVos = menuMapper.selectMenuPage(menuVoPage);
        return ResponseResult.success(menuVos);
    }

    /**
     * 根据类型查询或者菜的id查询
     * @param type
     * @return
     */
    @Override
    public ResponseResult<List<MenuVo>> selectMenuByType(Integer type,Long menuId) {
        List<MenuVo> menuVos = menuMapper.selectMenuList(type,menuId);
        if (ObjectUtils.isEmpty(menuVos)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return ResponseResult.success(menuVos);
    }

    /**
     * 删除图片
     * @param imgId
     * @return
     */
    @Override
    public ResponseResult<String> deleteImg(Long imgId) {
        Integer integer = menuMapper.deleteImg(imgId);
        if (ObjectUtils.isEmpty(integer)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return ResponseResult.success("删除成功");
    }

    /**
     * 添加图片
     * @param imgQo
     * @return
     */
    @Override
    public ResponseResult<String> addImg(ImgQo imgQo) {
        Long mId = imgQo.getMId();
        if (ObjectUtils.isEmpty(mId)){
            mId =  orderUtils.getImgId();
        }
        List<MultipartFile> file = imgQo.getFile();
        for (MultipartFile multipartFile : file) {
            String url = upload.uploadImg(multipartFile);
            if (ObjectUtils.isEmpty(url)){
                throw new ServiceException(ResultCode.FAIL_TO_UPLOAD);
            }
            Img img = new Img();
            img.setMId(mId);
            img.setUrl(url);
            Integer integer = menuMapper.addImg(img);
            if (ObjectUtils.isEmpty(integer)){
                throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
            }
        }
        return ResponseResult.success("添加成功");
    }

    /**
     * 编辑菜
     * @param menuQo
     * @return
     */
    @Override
    public ResponseResult<String> updateMenu(MenuQo menuQo) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuQo,menu);
        menu.setMId(menuQo.getMenuId());
        Integer integer = menuMapper.updateMenu(menu);
        if (ObjectUtils.isEmpty(integer)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return ResponseResult.success("修改成功");
    }

    /**
     * 是否售空，0代表售空，1代表正常，2代表审核中
     * @param menuQo
     * @return
     */
    @Override
    public ResponseResult<String> updateStatus(MenuQo menuQo) {
        Integer integer = menuMapper.updateStatus(menuQo);
        if (ObjectUtils.isEmpty(integer)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return ResponseResult.success("修改成功");
    }

    /**
     * 添加新菜品
     * @param menuQo
     * @return
     */
    @Override
    public ResponseResult<String> addMenu(MenuQo menuQo) {
        Long imgId = orderUtils.getImgId();
        List<MultipartFile> file = menuQo.getFile();
        if (!ObjectUtils.isEmpty(file)){
            for (MultipartFile multipartFile : file) {
                String url = upload.uploadImg(multipartFile);
                if (ObjectUtils.isEmpty(url)){
                    throw new ServiceException(ResultCode.FAIL_TO_UPLOAD);
                }
                Img img = new Img();
                img.setMId(imgId);
                img.setUrl(url);
                Integer integer = menuMapper.addImg(img);
                if (ObjectUtils.isEmpty(integer)){
                    throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
                }
            }
        }
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuQo,menu);
        menu.setImg(imgId);
        int insert = menuMapper.insert(menu);
        if (ObjectUtils.isEmpty(insert)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return ResponseResult.success("添加成功");
    }

    @Override
    public ResponseResult<String> deleteMenu(List<Long> id) {
        Integer integer = menuMapper.deleteMenu(id);
        if (ObjectUtils.isEmpty(integer)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        List<Menu> imgId = menuMapper.getImgId(id);
        if (ObjectUtils.isEmpty(imgId)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        List<Long> longs = new ArrayList<>();
        for (Menu menu : imgId) {
            longs.add(menu.getImg());
        }
        Integer integer1 = menuMapper.deleteImgs(longs);
        if (ObjectUtils.isEmpty(integer1)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return ResponseResult.success("删除成功");
    }

    /**
     * 点赞
     * @param menuId 菜品id
     * @return
     */
    @Override
    public ResponseResult<String> addPraiseNumber(Long menuId) {
        Integer integer = menuMapper.addPraiseNumber(menuId);
        if (ObjectUtils.isEmpty(integer)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return ResponseResult.success("点赞成功");
    }

    /**
     * 根据状态查询
     * @param status
     * @return
     */
    @Override
    public List<MenuVo> selectMenuBystatus(Integer status) {
        List<MenuVo> menuVos = menuMapper.selectMenuBystatus(status);
        if (ObjectUtils.isEmpty(menuVos)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return menuVos;
    }

    /**
     * 根据点赞排行_倒序
     * @return
     */
    @Override
    public ResponseResult<List<MenuVo>> rankingList() {
        List<MenuVo> menuVos = menuMapper.rankingList();
        if (ObjectUtils.isEmpty(menuVos)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return ResponseResult.success(menuVos);
    }

    /**
     * 根据名字模糊查询
     * @param name
     * @return
     */
    @Override
    public ResponseResult<List<MenuVo>> selectMenuByName(String name) {
        String menuName = "%"+name+"%";
        List<MenuVo> menuVos = menuMapper.selectMenuByName(menuName);
        if (ObjectUtils.isEmpty(menuVos)){
            throw new ServiceException(ResultCode.BUSINESS_UNKNOW_ERROR);
        }
        return ResponseResult.success(menuVos);
    }

}
